#include "TextureLoader.h"
#include "Texture.h"

TextureLoader::TextureLoader()
{
	ilInit();  
	iluInit();  
	ilutInit();  
	ilutRenderer(ILUT_OPENGL);
}

TextureLoader::~TextureLoader()
{
	std::map< std::string, unsigned int>::iterator it;
	for( it = m_textures.begin(); it != m_textures.end(); it++ )
	{
		glDeleteTextures(1, (GLuint *) &it->second);
	}
}

void TextureLoader::reload()
{
	std::vector<Texture*>::iterator it;
	for( it = m_textureObjects.begin(); it != m_textureObjects.end(); it++ )
		(*it)->forceReload();

	m_textures.clear();
}

unsigned int TextureLoader::loadTexture( const std::string& filename, Texture* obj )
{
	m_textureObjects.push_back(obj);

	std::map< std::string, unsigned int>::iterator it = m_textures.find(filename);
	if( it != m_textures.end() ) 
		return it->second;

	m_textures[filename] = ilutGLLoadImage((char *) filename.c_str());
	printf("Loading texture: %s\n", filename.c_str());
	return m_textures[filename];
}